//
//  ExtractCoinViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/25.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class ExtractCoinViewModel: BaseViewModel {
    @objc dynamic  var refreshBool = false //监听的状态
    var selectBool = false
    var dataArr:Array<CoinCodeModel> = []
    var coinCode:String = ""
    var drawAdd:String = ""
    lazy var addressRequest: GetCoinAdressRequest = {
        let api = GetCoinAdressRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    lazy var deleteRequest: DelegateAddressRequest = {
        let api = DelegateAddressRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    func archiveCoinAddress() {
        addressRequest.coinCode = coinCode
        CYMBShow.openIndicator(message: nil, view: self.controller?.view)
        self.request(request: addressRequest, success: {[weak self] (result) in
            CYMBShow.colseIndicator(view: self?.controller?.view)
            self?.dataArr.removeAll()
            guard let dic = result else {
                return
            }
            guard let array = dic["data"] as? [[String:Any]] else {
                return
            }
            for smalDic in array {
                let model = CoinCodeModel(JSON: smalDic)
                if model != nil {
                    self?.dataArr.append(model!)
                }
            }
            self?.refreshBool = true
            
        }) { (response) in
            if response.errorMsg.count != 0 {
            CYMBShow.showMessage(response.errorMsg)
            }
            CYMBShow.colseIndicator(view: self.controller?.view)
        }
    }
    
    func delectAddress() {
        self.request(request: deleteRequest, success: { (result) in
            self.archiveCoinAddress()
            NotificationCenter.default.post(name: NSNotification.Name.Exchange.addressNumberChange, object: nil)
        }) { (response) in
            CYMBShow.showMessage(response.errorMsg)
        }
    }
    
}
extension ExtractCoinViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        let block = self.successBlockDic.object(forKey: request.path())
        if ((block as? success) != nil) {
            (block as! success)(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        let block = self.failBlockDic.object(forKey: request.path())
        if ((block as? fail) != nil) {
            (block as! fail)(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? DelegateAddressRequest {
            pramas.updateValue(coinCode, forKey: "coinCode")
            pramas.updateValue(drawAdd, forKey: "drawAdd")
        }
        return pramas
    }


}
